home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1 / Nebula One.iso / Internet / WWW / Perl_WWW_Utilities / total / links / links.pl < prev    next >
Encoding:
Perl Script  |  1996-01-01  |  4.0 KB  |  148 lines

  1. #!/usr/bin/perl
  2. # Free For All Link Script
  3. # Created by Matt Wright        (mattw@misha.net)
  4. # Created On: 5/14/95           Last Modified: 10/15/95
  5. # Version: 2.1
  6.  
  7. # Define Variables
  8. $filename = "/home/mattw/public_html/links/newlinks.html";
  9. $linksurl = "http://your.host.xxx/links/newlinks.html";
  10. $linkspl = "http://your.host.xxx/cgi-bin/links.pl";
  11.  
  12. $datecom = '/usr/bin/date';
  13. $date = `$datecom +"%r on %A, %B %d, %Y %Z"`; chop($date);
  14.  
  15. # Get the input
  16. read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
  17.  
  18. # Split the name-value pairs
  19. @pairs = split(/&/, $buffer);
  20.  
  21. foreach $pair (@pairs) {
  22.    ($name, $value) = split(/=/, $pair);
  23.  
  24.    $value =~ tr/+/ /;
  25.    $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
  26.    $value =~ s/<([^>]|\n)*>//g;
  27.  
  28.    $FORM{$name} = $value;
  29. }
  30.  
  31. if ($FORM{'url'} eq 'http://') { &no_url; }
  32. &no_url unless $FORM{'url'};
  33. &no_title unless $FORM{'title'};
  34.  
  35. # Enter our tags and sections into an associative array
  36.  
  37. %sections = ("busi","Business","comp","Computers","educ","Education",
  38.          "ente","Entertainment","gove","Government",
  39.          "pers","Personal","misc","Miscellaneous");
  40.  
  41. # Suck previous link file into one big string
  42. $response = `grep '<li><a href' $filename`;
  43. @data = split(/\n/,$response);
  44.  
  45. $i=1;
  46.  
  47. foreach $line (@data) { # For every line in our data
  48.   $i++;
  49. }
  50.  
  51. open (FILE,"$filename");
  52. @LINES=<FILE>;
  53. close(FILE);
  54. $SIZE=@LINES;
  55.  
  56. open (FILE,">$filename");
  57. for ($a=0;$a<=$SIZE;$a++) {
  58.    $_=$LINES[$a];
  59.    if (/<!--number-->/) {
  60.       print FILE "<!--number--><b>There are <i>$i</i> links on this ";
  61.       print FILE "page.</b><br>\n";
  62.    }
  63.    elsif (/<!--time-->/) {
  64.       print FILE "<!--time--><b>Last link was added at $date</b><hr>\n";
  65.    }
  66.    else { 
  67.       print FILE $_;
  68.    }
  69. }
  70. close (FILE);
  71.  
  72. open (FILE,"$filename");
  73.  
  74. while (<FILE>) {
  75.    $raw_data .=  $_;
  76. }
  77.  
  78. close(FILE);
  79.  
  80. # Make a normal array out of this data, one line per entry.  NOTE: This
  81. # eats up our newlines, so be sure to add them back when we print back to
  82. # the file.
  83.  
  84. undef $/;
  85. @proc_data = split(/\n/,$raw_data);
  86.  
  87. # Open Link File to Output
  88. open (FILE,">$filename");
  89.  
  90. foreach $line (@proc_data) { # For every line in our data
  91.  
  92.    print FILE "$line\n";   # Print the line.  We have to do this no
  93.                   # matter what, so let's get it over with.
  94.  
  95.    foreach $tag (keys(%sections)) { # For every tag 
  96.       if ( ($FORM{section} eq $sections{$tag}) && 
  97.          ($line =~ /<!--$tag-->/) ) {
  98.  
  99.          print FILE "<li><a href=\"$FORM{'url'}\">$FORM{'title'}</a>\n"; 
  100.       }
  101.    }
  102. }
  103.  
  104. close (FILE);
  105.  
  106. # Return Link File
  107. print "Location: $linksurl\n\n";
  108.  
  109. sub no_url {
  110.    print "Content-type: text/html\n\n";
  111.    print "<html><head><title>NO URL</title></head>\n";
  112.    print "<body><h1>ERROR - NO URL</h1>\n";
  113.    print "You forgot to enter a url you wanted added to the Free for ";  
  114.    print "all link page.<p>\n";
  115.    print "<form method=POST action=\"$linkspl\">\n";
  116.    print "<input type=hidden name=\"title\" value=\"$FORM{'title'}\">\n";
  117.    print "<input type=hidden name=\"section\""; 
  118.    print "value=\"$FORM{'section'}\">\n";
  119.    print "URL: <input type=text name=\"url\" size=50><p>\n";
  120.    print "<input type=submit> * <input type=reset>\n";
  121.    print "<hr>\n";
  122.    print "<a href=\"$linksurl\">Back to the Free for all Link"; 
  123.    print "Page</a>\n";
  124.    print "</form></body></html>\n";
  125.  
  126.    exit;
  127. }
  128.  
  129. sub no_title {
  130.    print "Content-type: text/html\n\n";
  131.    print "<html><head><title>NO TITLE</title></head>\n";
  132.    print "<body><h1>ERROR - NO TITLE</h1>\n";
  133.    print "You forgot to enter a title you wanted added to the Free for ";
  134.    print "all link page.<p>\n";
  135.    print "<form method=POST action=\"$linkspl\">\n";
  136.    print "<input type=hidden name=\"url\" value=\"$FORM{'url'}\">\n"; 
  137.    print "<input type=hidden name=\"section\"";
  138.    print "value=\"$FORM{'section'}\">\n";
  139.    print "TITLE: <input type=text name=\"title\" size=50><p>\n";
  140.    print "<input type=submit> * <input type=reset>\n";
  141.    print "<hr>\n";
  142.    print "<a href=\"$linksurl\">Back to the free for all links";
  143.    print "page</a>\n";
  144.    print "</form></body></html>\n";
  145.  
  146.    exit;
  147. }
  148.